import sys
import os
from SimpleXMLRPCServer import SimpleXMLRPCServer
import pexpect

##########################################
home_dir = os.path.expanduser("~")
password = open( home_dir + "/.qlive/global-config/config/qlive.auth", "r").readline().strip()

def updateAddress(if_name, new_ip, net_mask) :
	print "qlive > update address"

	p = pexpect.spawn("sudo ifconfig %s %s netmask %s up" % (if_name,new_ip, net_mask))
	i = p.expect([".ssword:*",pexpect.EOF])
	p.sendline(password)
	p.expect(pexpect.EOF)
	return True

def updateDefaultGateway(gateway):
	print "qlive > update gateway"

	"sudo route add default gw 192.168.1.254"
	p = pexpect.spawn("sudo route add default gw %s" % (gateway))
	i = p.expect([".ssword:*",pexpect.EOF])
	p.sendline(password)
	p.expect(pexpect.EOF)
	return True

def testSound():
	command = "aplay question.wav"
	os.command(command)
	return True

if __name__ == "__main__":
	
	"""
	if_name = sys.argv[1]
	new_ip = sys.argv[2]
	net_mask = sys.argv[3]
	
	updateAddress(if_name, new_ip,net_mask)
	"""

	server = SimpleXMLRPCServer( ("localhost", 9870) )
	server.register_function(updateAddress)
	server.register_function(updateDefaultGateway)
	server.register_function(testSound)
	
	print "qlive -> [address] start service@localhost 9870..."
	server.serve_forever()

